package
{
	import com.greensock.TimelineMax;
	import com.greensock.TweenMax;
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.filters.DropShadowFilter;
	import flash.geom.Vector3D;

	public class FX
	{
		public function FX()
		{
		}
		
		public static function fadeInOutTo( mc:Object, val:Number, f:Function = null  ):void
		{
			TweenMax.to( mc, ApplicationFacade.SPEED, { alpha:val, onComplete:f } );
		}
		
		public static function translateMultipleY( arr:Array, val:Number, f:Function = null ):void
		{
			
			var timeline:TimelineMax = new TimelineMax( { paused:true, onComplete:f } );
			
			timeline.appendMultiple( TweenMax.allTo( arr, ApplicationFacade.SPEED, {y:val} ) );
			
			timeline.play();
		}
		
		public static function spawnFrame(mc:Object, vec:Vector.<Vector3D>, f:Function = null, vars:Object = null):void
		{
			var tl:TimelineMax = new TimelineMax( { paused:true, onComplete:f });
			var key:int = 0;
			
			var g:Graphics = mc.graphics;
			g.lineStyle( vars.LINE_WEIGHT, vars.LINE_COLOR );
			
			var m:Sprite;
			var arr:Array = new Array();
			
			var i:uint;
			for( i = 0; i < vec.length; i++ )
			{
				m = new Sprite();
				arr.push( new Sprite() );
				arr[i].x = vec[i].x;
				arr[i].y = vec[i].y;
				
				if( i == vec.length-1)
				{
					tl.append( TweenMax.to( arr[i],  ApplicationFacade.SPEED,
						{ 	x:vec[0].x,
							y:vec[0].y, 
							onUpdate:onUpdateHandler,
							onComplete:onCompleteHandler } ) );
				}
				else
				{
					tl.append( TweenMax.to( arr[i],  ApplicationFacade.SPEED,
						{ 	x:vec[i+1].x,
							y:vec[i+1].y, 
							onUpdate:onUpdateHandler,
							onComplete:onCompleteHandler } ) );
				}
				
			}
			
			tl.play();
			
			function onUpdateHandler():void
			{
				if(key == vec.length) return;
				
				var n:uint;
				for( n = 0; n < key; n++ )
				{
					g.moveTo( vec[n].x, vec[n].y );
					g.lineTo( arr[n].x, arr[n].y );
				}
				
				g.moveTo( vec[key].x, vec[key].y );
				g.lineTo( arr[key].x, arr[key].y );
			}
			
			function onCompleteHandler():void
			{
				key++;
			}
		}
		
		public static function spawnHeader( mc:Object, labelA:Object, labelB:Object, vec:Vector.<Vector3D>, f:Function = null, vars:Object = null):void
		{
			var tl:TimelineMax = new TimelineMax( { paused:true });
			var key:int = 0;
			
			var g:Graphics = mc.graphics;
			g.lineStyle( vars.LINE_WEIGHT, vars.LINE_COLOR, 0.1 );
			
			var m:Sprite;
			var arr:Array = new Array();
			
			var i:uint;
			for( i = 0; i < vec.length; i++ )
			{
				m = new Sprite();
				arr.push( new Sprite() );
				arr[i].x = vec[i].x;
				arr[i].y = vec[i].y;
				
				if( i == vec.length-1)
				{
					tl.append( TweenMax.to( arr[i],  ApplicationFacade.SPEED,
											{ 	x:vec[0].x,
												y:vec[0].y, 
												onUpdate:onUpdateHandler,
												onComplete:onCompleteHandler } ) );
				}
				else
				{
					tl.append( TweenMax.to( arr[i],  ApplicationFacade.SPEED,
											{ 	x:vec[i+1].x,
												y:vec[i+1].y, 
												onUpdate:onUpdateHandler,
												onComplete:onCompleteHandler } ) );
				}
				
			}
			
			tl.append( TweenMax.to( labelA,  ApplicationFacade.SPEED, { alpha:1 } ) );
			tl.append( TweenMax.to( labelB,  ApplicationFacade.SPEED, { alpha:1 } ) );
			
			tl.play();
			
			function onUpdateHandler():void
			{
				if(key == vec.length) return;
				
				var n:uint;
				for( n = 0; n < key; n++ )
				{
					g.moveTo( vec[n].x, vec[n].y );
					g.lineTo( arr[n].x, arr[n].y );
				}
				
				g.moveTo( vec[key].x, vec[key].y );
				g.lineTo( arr[key].x, arr[key].y );
			}
			
			function onCompleteHandler():void
			{
				key++;
			}
		}
		
		public static function colapseExpand( mc:Object, val:Number, f1:Function = null, f2:Function = null ):void
		{
			TweenMax.to( mc, ApplicationFacade.SPEED, { height:val, onUpdate:f1, onComplete:f2 } );
		}
		
		public static function fadeIn( mc:Object ):void
		{
			TweenMax.to( mc, ApplicationFacade.SPEED, { alpha:1 } );
		}
		
		public static function scaleIn( mc:Object, f:Function = null, f1:Function = null ):void
		{
			TweenMax.to( mc, ApplicationFacade.SPEED, { scaleX:1, scaleY:1, onUpdate:f1, onComplete:f } );
		}
		
		public static function scaleOut( mc:Object, f:Function = null, f1:Function = null ):void
		{
			TweenMax.to( mc, ApplicationFacade.SPEED, { scaleX:0, scaleY:0, alpha:0, onUpdate:f1, onComplete:f } );
		}
		
		public static function fadeOutInterval( arr:Array, val:Number, f:Function = null):void
		{
			var timeline:TimelineMax = new TimelineMax( { paused:true } );
			
			timeline.appendMultiple( TweenMax.allTo( arr, ApplicationFacade.SPEED, {alpha:0}, 2 ) );
			
			timeline.play();
		}
		
		public static function fadeOut( mc:Object, f:Function = null ):void
		{
			TweenMax.to( mc, ApplicationFacade.SPEED, { alpha:0, onComplete:f } );
		}
		
		public static function tint( mc:Object, color:Number ):void
		{
			TweenMax.to( mc, ApplicationFacade.SPEED, { tint:color } );
		}
		
		public static function addShadow( mc:Object, inner:Boolean = false ):void
		{
			mc.filters = [ new DropShadowFilter( 1, 45, 0x111111, 0.5, 1, 1, 1, 3, inner ) ];
		}
		
		public static function removeFilters( mc:Object ):void
		{
			mc.filters = [];
		}
		
		public static function translateY( mc:Object, val:Number ):void
		{
			TweenMax.to( mc, ApplicationFacade.SPEED, { y:val } );
		}
		
		public static function shuffleFadeIn( c:Array, f:Function = null ):void
		{			
			var arr2:Array = [];
			var i:uint;
			for( i = 0; i < c.length; i++ )
			{
				c[i].alpha = 0;
			}
			
			while (c.length > 0)
			{
				arr2.push( c.splice( Math.round(Math.random() * ( c.length - 1 )), 1 )[0] );
			}
			var timeline:TimelineMax = new TimelineMax( { paused:true } );
			
			timeline.appendMultiple( TweenMax.allTo( arr2, ApplicationFacade.SPEED, {alpha:1}, 0.1 ) );
			
			timeline.play();
		}
		public static function cascadeFadeIn( c:Array, f:Function = null ):void
		{
			var i:uint;
			for( i = 0; i < c.length; i++ )
			{
				c[i].alpha = 0;
			}
			var timeline:TimelineMax = new TimelineMax( { paused:true } );
			
			timeline.appendMultiple( TweenMax.allTo( c, ApplicationFacade.SPEED, {alpha:1}, 0.05 ) );
			
			timeline.play();
		}
	}
}